package com.ruoyix.im.controller;

import java.util.Collections;
import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyix.im.domain.bo.ImChatUserMessageBo;
import com.ruoyix.im.domain.vo.ImChatUserMessageVo;
import com.ruoyix.im.service.IImChatUserMessageService;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyix.common.annotation.RepeatSubmit;
import com.ruoyix.common.annotation.Log;
import com.ruoyix.common.core.controller.BaseController;
import com.ruoyix.common.core.domain.PageQuery;
import com.ruoyix.common.core.domain.R;
import com.ruoyix.common.core.validate.AddGroup;
import com.ruoyix.common.core.validate.EditGroup;
import com.ruoyix.common.enums.BusinessType;
import com.ruoyix.common.utils.poi.ExcelUtil;

import com.ruoyix.common.core.page.TableDataInfo;

/**
 * 单聊记录
 *
 * @author ruoyi
 * @date 2024-05-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/weblog/chatUserMessage")
public class ImChatUserMessageController extends BaseController {

    private final IImChatUserMessageService iImChatUserMessageService;

    /**
     * 查询单聊记录列表
     */
    @SaCheckPermission("weblog:chatUserMessage:list")
    @GetMapping("/list")
    public TableDataInfo<ImChatUserMessageVo> list(ImChatUserMessageBo bo, PageQuery pageQuery) {
        return iImChatUserMessageService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出单聊记录列表
     */
    @SaCheckPermission("weblog:chatUserMessage:export")
    @Log(title = "单聊记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ImChatUserMessageBo bo, HttpServletResponse response) {
        List<ImChatUserMessageVo> list = iImChatUserMessageService.queryList(bo);
        ExcelUtil.exportExcel(list, "单聊记录", ImChatUserMessageVo.class, response);
    }

    /**
     * 获取单聊记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("weblog:chatUserMessage:query")
    @GetMapping("/{id}")
    public R<ImChatUserMessageVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iImChatUserMessageService.queryById(id));
    }

    /**
     * 新增单聊记录
     */
    @SaCheckPermission("weblog:chatUserMessage:add")
    @Log(title = "单聊记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ImChatUserMessageBo bo) {
        return toAjax(iImChatUserMessageService.insertByBo(bo));
    }

    /**
     * 修改单聊记录
     */
    @SaCheckPermission("weblog:chatUserMessage:edit")
    @Log(title = "单聊记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ImChatUserMessageBo bo) {
        return toAjax(iImChatUserMessageService.updateByBo(bo));
    }

    /**
     * 删除单聊记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("weblog:chatUserMessage:remove")
    @Log(title = "单聊记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iImChatUserMessageService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 获取系统消息（只获取前十条）
     */
    @GetMapping("/listSystemMessage")
    public R<Page> listSystemMessage(@RequestParam(value = "current", defaultValue = "1") Long current,
                                                @RequestParam(value = "size", defaultValue = "10") Long size) {
        return iImChatUserMessageService.listSystemMessage(current, size);
    }




    /**
     * 管理员添加系统消息
     */
    @GetMapping("/saveSystemMessage")
    public R<Void> saveSystemMessage(@RequestParam("content") String content) {
        iImChatUserMessageService.saveSystemMessage(content);
        return R.ok();
    }




    /**
     * 删除系统消息
     */
    @GetMapping("/deleteSystemMessage")
    public R<Void> deleteSystemMessage(@RequestParam("id") Integer id) {
        iImChatUserMessageService.removeById(id);
        return R.ok();
    }

    /**
     * 获取朋友消息（只获取前四十条）
     */
    @GetMapping("/listFriendMessage")
    public R<Page> listFriendMessage(@RequestParam(value = "current", defaultValue = "1") Long current,
                                                @RequestParam(value = "size", defaultValue = "40") Long size,
                                                @RequestParam(value = "friendId") Long friendId) {
        return iImChatUserMessageService.listFriendMessage(current, size, friendId);
    }


    @GetMapping("/getChatUserMessageListByUserId")
    public R<Map<Long, List<ImChatUserMessageVo>>> getChatUserMessageListByUserId(Long userId){
        Map<Long, List<ImChatUserMessageVo>> chatUserMessageListByUserId = iImChatUserMessageService.getChatUserMessageListByUserId(userId);
        return R.ok(chatUserMessageListByUserId);
    }
    @GetMapping("/setMessageRead")
    public R<Void> setMessageRead(Long fromId,Long toId){
        iImChatUserMessageService.setMessageRead(fromId, toId);
        return R.ok();
    }


}
